package morenoapp.model.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import morenoapp.model.data.MorenoAnswerFace;
import morenoapp.model.data.MorenoAnswerItemFace;
import morenoapp.model.data.MorenoQuestionFace;
import morenoapp.model.data.MorenoQuestionItemFace;
import morenoapp.model.data.MorenoRespondentFace;

/**
 * @author dmitry.mamonov
 */
class MorenoAnswerImpl extends MorenoModelObject implements MorenoAnswerFace{
    protected MorenoRespondentFace respondent;
    protected MorenoQuestionFace question;
    protected Map<MorenoQuestionItemFace, MorenoAnswerItemFace> answerItemsMap = new HashMap();

    MorenoAnswerImpl(MorenoRespondentImpl _respondent, MorenoQuestionFace _question) {
        super(_respondent.getModel());
        this.respondent = _respondent;
        this.question = _question;
    }

    @Override
    public MorenoRespondentFace getRespondent() {
        return this.respondent;
    }

    @Override
    public MorenoQuestionFace getQuestion() {
        return this.question;
    }

    @Override
    public List<MorenoAnswerItemFace> getAnswerItems() {
        return new ArrayList<MorenoAnswerItemFace>(answerItemsMap.values());
    }

    @Override
    public MorenoAnswerItemFace getAnswerItemByQuestionItem(MorenoQuestionItemFace _questionItem) {
        if (!answerItemsMap.containsKey(_questionItem)){
            answerItemsMap.put(_questionItem, new MorenoAnswerItemImpl(this, _questionItem, (MorenoRespondentFace)null));
        }
        return answerItemsMap.get(_questionItem);
    }

    @Override
    public MorenoAnswerItemFace getAnswerItemByRespondent(MorenoRespondentFace _target) {
        for(MorenoAnswerItemFace answerItem:answerItemsMap.values()){
            if (_target.equals(answerItem.getTarget())){
                return answerItem;
            }
        }
        return null;
    }
}
